<?php

namespace App\Models\Product;

use App\Models\Config as ConfigModel;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Product extends Model
{
    use HasFactory;

    use SoftDeletes;

    protected $table = 'product';

    // 自动写入时间戳
    public $timestamps = true;

    // 时间类型
    protected $dateFormat = 'U';

    // 自定义创建时间字段
    const CREATED_AT = 'createtime';

    // 自定义更新时间字段
    const UPDATED_AT = null;

    // 软删除字段
    const DELETED_AT = 'deletetime';

    protected $appends = [
        'thumb_cdn',
        'thumbs_cdn'
    ];

    public function getThumbCdnAttribute()
    {
        $cdn = ConfigModel::where('name','url')->value('value');

        $url = $cdn . '/shop/product/product/thumb';

        $thumb = httpRequest($url,['proid' => $this->id]);

        return $thumb;
    }

    public function getThumbsCdnAttribute()
    {
        $cdn = ConfigModel::where('name','url')->value('value');

        $url = $cdn . '/shop/product/product/thumbs';

        $thumbs = httpRequest($url,['proid' => $this->id]);
    
        $thumbs = json_decode($thumbs,true);

        return $thumbs;
    }
}
